<?php
/**
 * APIObjectEditTest
 */
class APIObjectEditTest extends APIWebServiceTest
{
    public static function setUpBeforeClass () {
        self::_setUpBeforeClass("APIObjectEditTest");
    }
    public static function tearDownAfterClass () {
        self::_tearDownAfterClass();
    }

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    public function setUp () {}

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    public function tearDown () {}

    /**
     * testEditObjectsREST01
     */
    public function testEditObjectsREST01()
    {
        global $context;
        $appdb = $context->database("app");
        $appdb->delete('tracked_url',array('url'=>'tbd'));

        #########################################################################
        # TEST: POST /object/app/tracked_url
        #########################################################################
        $request_json = <<<EOF
{
   "url_title" : "",
   "url_category" : "",
   "url" : "",
   "url_type" : "",
   "auth_required" : "",
   "url_id" : "",
   "vendor" : ""
}
EOF;
        $request  = json_decode($request_json, true);
        $response = array();
        $this->_callWebService('POST', '/object/app/tracked_url', $request, $response);
        #{
        #   "success" : true,
        #   "data" : {
        #      "create_dttm" : "2014-02-13 22:41:26",
        #      "url_type" : "",
        #      "auth_required" : "N",
        #      "url_title" : "",
        #      "url_category" : "",
        #      "url" : "tbd",
        #      "url_id" : "663",
        #      "vendor" : ""
        #   },
        #   "nrows" : 1,
        #   "message" : "Created or updated row"
        #}

        $this->assertTrue(isset($response['success']), 'tracked_url: success is set');
        $this->assertEquals($response['success'], true, 'tracked_url: success = [true]');
        $this->assertTrue(isset($response['data']), 'tracked_url: data is set');
        $this->assertTrue(isset($response['nrows']), 'tracked_url: nrows is set');
        $this->assertEquals($response['nrows'], '1', 'tracked_url: nrows = [1]');
        $this->assertTrue(isset($response['message']), 'tracked_url: message is set');
        $this->assertEquals($response['message'], 'Created or updated row', 'tracked_url: message = [Created or updated row]');
        $url_id = $response['data']['url_id'];

        #########################################################################
        # TEST: PUT /object/app/tracked_url/663
        #########################################################################
        $request_json = <<<EOF
{
   "url_category" : "1",
   "url_id" : "663",
   "url_type" : "Library Articles"
}
EOF;
        $request  = json_decode($request_json, true);
        $request['url_id'] = $url_id;
        $response = array();
        $this->_callWebService('PUT', "/object/app/tracked_url/$url_id", $request, $response);
        $expected_response_json = <<<EOF
{
   "success" : true,
   "table" : "tracked_url",
   "nrows" : 1,
   "message" : "Updated 1 row",
   "key" : "663"
}
EOF;
        $expected_response  = json_decode($expected_response_json, true);
        $expected_response['key'] = $url_id;
        $this->assertDeeply($expected_response, $response, "PUT tracked_url/$url_id response");

        $this->assertTrue(isset($response['success']), "tracked_url/$url_id: success is set");
        $this->assertEquals($response['success'], true, "tracked_url/$url_id: success = [true]");
        $this->assertTrue(isset($response['table']), "tracked_url/$url_id: table is set");
        $this->assertEquals($response['table'], 'tracked_url', "tracked_url/$url_id: table = [tracked_url]");
        $this->assertTrue(isset($response['nrows']), "tracked_url/$url_id: nrows is set");
        $this->assertEquals($response['nrows'], '1', "tracked_url/$url_id: nrows = [1]");
        $this->assertTrue(isset($response['message']), "tracked_url/$url_id: message is set");
        $this->assertEquals($response['message'], 'Updated 1 row', "tracked_url/$url_id: message = [Updated 1 row]");
        $this->assertTrue(isset($response['key']), "tracked_url/$url_id: key is set");
        $this->assertEquals($response['key'], $url_id, "tracked_url/$url_id: key = [$url_id]");

        #########################################################################
        # TEST: PUT /object/app/tracked_url/663
        #########################################################################
        $request_json = <<<EOF
{
   "url_title" : "Testing",
   "url" : "http://www.testing.com/",
   "url_id" : "663"
}
EOF;
        $request  = json_decode($request_json, true);
        $request['url_id'] = $url_id;
        $response = array();
        $this->_callWebService('PUT', "/object/app/tracked_url/$url_id", $request, $response);
        #{
        #   "success" : true,
        #   "table" : "tracked_url",
        #   "nrows" : 1,
        #   "message" : "Updated 1 row",
        #   "key" : "663"
        #}

        $this->assertTrue(isset($response['success']), "tracked_url/$url_id: success is set");
        $this->assertEquals($response['success'], true, "tracked_url/$url_id: success = [true]");
        $this->assertTrue(isset($response['table']), "tracked_url/$url_id: table is set");
        $this->assertEquals($response['table'], 'tracked_url', "tracked_url/$url_id: table = [tracked_url]");
        $this->assertTrue(isset($response['nrows']), "tracked_url/$url_id: nrows is set");
        $this->assertEquals($response['nrows'], '1', "tracked_url/$url_id: nrows = [1]");
        $this->assertTrue(isset($response['message']), "tracked_url/$url_id: message is set");
        $this->assertEquals($response['message'], 'Updated 1 row', "tracked_url/$url_id: message = [Updated 1 row]");
        $this->assertTrue(isset($response['key']), "tracked_url/$url_id: key is set");
        $this->assertEquals($response['key'], $url_id, "tracked_url/$url_id: key = [$url_id]");

        #########################################################################
        # TEST: PUT /object/app/tracked_url/663
        #########################################################################
        $request_json = <<<EOF
{
   "url_title" : "Testing2",
   "url_id" : "663"
}
EOF;
        $request  = json_decode($request_json, true);
        $request['url_id'] = $url_id;
        $response = array();
        $this->_callWebService('PUT', "/object/app/tracked_url/$url_id", $request, $response);
        #{
        #   "success" : true,
        #   "table" : "tracked_url",
        #   "nrows" : 1,
        #   "message" : "Updated 1 row",
        #   "key" : "663"
        #}

        $this->assertTrue(isset($response['success']), "tracked_url/$url_id: success is set");
        $this->assertEquals($response['success'], true, "tracked_url/$url_id: success = [true]");
        $this->assertTrue(isset($response['table']), "tracked_url/$url_id: table is set");
        $this->assertEquals($response['table'], 'tracked_url', "tracked_url/$url_id: table = [tracked_url]");
        $this->assertTrue(isset($response['nrows']), "tracked_url/$url_id: nrows is set");
        $this->assertEquals($response['nrows'], '1', "tracked_url/$url_id: nrows = [1]");
        $this->assertTrue(isset($response['message']), "tracked_url/$url_id: message is set");
        $this->assertEquals($response['message'], 'Updated 1 row', "tracked_url/$url_id: message = [Updated 1 row]");
        $this->assertTrue(isset($response['key']), "tracked_url/$url_id: key is set");
        $this->assertEquals($response['key'], $url_id, "tracked_url/$url_id: key = [$url_id]");

        #########################################################################
        # TEST: DELETE /object/app/tracked_url/663
        #########################################################################
        $request_json = <<<EOF
{
   "url_id" : "663"
}
EOF;
        $request  = json_decode($request_json, true);
        $request['url_id'] = $url_id;
        $response = array();
        $this->_callWebService('DELETE', "/object/app/tracked_url/$url_id", $request, $response);
        #{
        #   "success" : true,
        #   "table" : "tracked_url",
        #   "nrows" : 1,
        #   "message" : "Deleted 1 row",
        #   "key" : "663"
        #}

        $this->assertTrue(isset($response['success']), "tracked_url/$url_id: success is set");
        $this->assertEquals($response['success'], true, "tracked_url/$url_id: success = [true]");
        $this->assertTrue(isset($response['table']), "tracked_url/$url_id: table is set");
        $this->assertEquals($response['table'], 'tracked_url', "tracked_url/$url_id: table = [tracked_url]");
        $this->assertTrue(isset($response['nrows']), "tracked_url/$url_id: nrows is set");
        $this->assertEquals($response['nrows'], '1', "tracked_url/$url_id: nrows = [1]");
        $this->assertTrue(isset($response['message']), "tracked_url/$url_id: message is set");
        $this->assertEquals($response['message'], 'Deleted 1 row', "tracked_url/$url_id: message = [Deleted 1 row]");
        $this->assertTrue(isset($response['key']), "tracked_url/$url_id: key is set");
        $this->assertEquals($response['key'], $url_id, "tracked_url/$url_id: key = [$url_id]");

    }

    /**
     * testEditObjectsREST02
     */
    public function testEditObjectsREST02()
    {
        global $context;
        $appdb = $context->database("app");
        $appdb->delete('email',array('email'=>'test7@foo.bar'));
        $group_id = $context->getAuthenticatedGroupId();
        $success_only = json_decode('{"success":true}',true);

        $request_json = <<<EOF
{"email":"test7@foo.bar","status":"A","inactive_email_id":0,"owner_group_id":0}
EOF;
        $request  = json_decode($request_json, true);
        $response = array();
        $this->_callWebService('POST', "/object/app/email", $request, $response);
        $expected_response_json = <<<EOF
{
    "success": true,
    "nrows": 1,
    "data": {
        "email": "test7@foo.bar",
        "status": "A",
        "inactive_email_id": "0",
        "owner_group_id": "_INTEGER_",
        "modify_dttm": "_DATETIME_",
        "create_dttm": "_DATETIME_",
        "email_id": "_INTEGER_"
    },
    "message": "Created or updated row"
}
EOF;
        $expected_response  = json_decode($expected_response_json, true);
        $this->assertDeeply($expected_response, $response, "Line#".__LINE__." POST /object/app/email (single object) response");
        $email_id = $response['data']['email_id'];
        $extra_fields = array(
            'data' => array(
                'owner_group_id' => $group_id,
                'email_id' => $email_id
            )
        );
        $this->assertDeeply($expected_response, $response, "Line#".__LINE__." POST /object/app/email (single object. more checks.) response", false);

        $request['email_id'] = $email_id;
        $response = array();
        $this->_callWebService('PUT', "/object/app/email", $request, $response);
        $this->assertDeeply($success_only, $response, "Line#".__LINE__." PUT /object/app/email (single object) response", false);

        $response = array();
        $this->_callWebService('DELETE', "/object/app/email", $request, $response);
        $this->assertDeeply($success_only, $response, "Line#".__LINE__." DELETE /object/app/email (single object) response", false);

        #$appdb->delete('email',array('email'=>'test7@foo.bar'));
        $request_json = <<<EOF
[{"email":"test7@foo.bar","status":"A","inactive_email_id":0,"owner_group_id":0}]
EOF;
        $request  = json_decode($request_json, true);
        $response = array();
        $this->_callWebService('POST', "/object/app/email", $request, $response);
        $expected_response_json = <<<EOF
{
    "success": true,
    "nrows": 1,
    "data": [
        {   
            "email": "test7@foo.bar",
            "status": "A",
            "inactive_email_id": "0",
            "owner_group_id": "_INTEGER_",
            "modify_dttm": "_DATETIME_",
            "create_dttm": "_DATETIME_",
            "email_id": "_INTEGER_"
        }
    ],
    "message": "Created or updated row"
}
EOF;
        $expected_response  = json_decode($expected_response_json, true);
        $this->assertDeeply($expected_response, $response, "Line#".__LINE__." POST /object/app/email (array of objects) response");
        $email_id = $response['data'][0]['email_id'];

        $request[0]['email_id'] = $email_id;
        $response = array();
        $this->_callWebService('PUT', "/object/app/email", $request, $response);
        $this->assertDeeply($success_only, $response, "Line#".__LINE__." PUT /object/app/email (array of objects) response", false);

        $response = array();
        $this->_callWebService('DELETE', "/object/app/email", $request, $response);
        $this->assertDeeply($success_only, $response, "Line#".__LINE__." DELETE /object/app/email (array of objects) response", false);
    }
}
